package cn.wxManage.goodsManage.service.impl;

import cn.common.common.model.ServerResponse;
import cn.wxManage.goodsManage.dao.GoodsCollectMapper;
import cn.wxManage.goodsManage.model.GoodsCollect;
import cn.wxManage.goodsManage.service.IGoodsCollectService;
import cn.wxManage.user.model.UserInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Zhang on 2019/1/25.
 */
@Service
public class GoodsCollectService implements IGoodsCollectService{
    @Autowired
    GoodsCollectMapper collectMapper;

    @Autowired
    HttpServletRequest request;
    @Override
    public ServerResponse<?> handleGoodsToCollect(Integer goodsId) {
        UserInfo userInfo = getUserInfo();
        if (collectMapper.checkIsCllect(userInfo.getUserid(),goodsId)){
            collectMapper.deleteUserCollect(userInfo.getUserid(),goodsId);
            return ServerResponse.createBySuccessMessage("取消成功");
        }else {
            GoodsCollect goodsCollect = new GoodsCollect();
            goodsCollect.setCreateTime(new Date());
            goodsCollect.setGoodsId(goodsId);
            goodsCollect.setUserid(getUserInfo().getUserid());
            collectMapper.insertSelective(goodsCollect);
            return ServerResponse.createBySuccessMessage("收藏成功");
        }
    }

    @Override
    public ServerResponse<?> getGoodsToCollect() {
        return null;
    }

    private UserInfo getUserInfo(){
        return (UserInfo) request.getAttribute("userInfo");
    }
}
